﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudFranceTriple.Models;
using WebMatrix.WebData;

namespace CloudFranceTriple.WebRole.Areas.UserManager.Controllers
{
    public class UserController : BaseController
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        private readonly IWebSecurityWrapper _webSecurity;
        public UserController()
        {
            this._webSecurity = new WebSecurityWrapper();
        }

        public UserController(IWebSecurityWrapper webSecurity)
            : base(webSecurity)
        {
            this._webSecurity = webSecurity;
        }

        //
        // POST: /UserManager/UserRoles/Link/UserId/RoleId

        [Authorize(Roles = "RoleHR")]
        public ActionResult SetUserPostCounter()
        {
            var uc = new CloudFranceTriple.WebRole.Models.UsersContext();
            var _members = uc.UserProfiles;
            foreach (CloudFranceTriple.WebRole.Models.UserProfile user in _members)
            {
                user.ForumMessagePostedCounter = db.ForumPostSet.Where(post => post.PostMessage_Author.ToLower() == user.UserName.ToLower()).Count();
                uc.Entry(user).State = System.Data.EntityState.Modified;
            }
            uc.SaveChanges();
            return RedirectToAction("Index", "User", new { area = "UserManager" });
        }

        //
        // GET: /UserManager/User/

        [Authorize(Roles = "RoleHR")]
        public ActionResult Index()
        {
            var query = db.UserProfile.OrderBy(user => user.UserName).ToList();
            var userTimeZoneInfo = Extension.GetUserTimeZoneInfo(_webSecurity.CurrentUserId);
            foreach (UserProfile user in query)
            {
                user.LastRequestDate = user.LastRequestDate.ToUserTimeZone(userTimeZoneInfo);
            }
            return View(query);
        }


        //
        // GET: /UserManager/Role/Details/5

        [Authorize(Roles = "RoleHR")]
        public ActionResult Details(int id = 0)
        {
            UserProfile userprofile = db.UserProfile.Find(id);
            if (userprofile == null)
            {
                return HttpNotFound();
            }
            return View(userprofile);
        }

        //
        // GET: /UserManager/User/Create

        [Authorize(Roles = "RoleHR")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /UserManager/User/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleHR")]
        public ActionResult Create(UserProfile userprofile)
        {
            if (ModelState.IsValid)
            {
                db.UserProfile.Add(userprofile);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(userprofile);
        }

        //
        // GET: /UserManager/User/Edit/5

        [Authorize(Roles = "RoleHR")]
        public ActionResult Edit(int id = 0)
        {
            var userprofile = new CloudFranceTriple.WebRole.Models.UsersContext().UserProfiles.Find(id);
            if (userprofile == null)
            {
                return HttpNotFound();
            }
            ViewBag.TimeZone = new SelectList(GetTimeZoneSelectList(), "Value", "Text", userprofile.TimeZone);
            return View(userprofile);
        }

        //
        // POST: /UserManager/User/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleHR")]
        public ActionResult Edit(CloudFranceTriple.WebRole.Models.UserProfile userprofile)
        {
            if (ModelState.IsValid & userprofile.UserId > 0)
            {
                var uc = new CloudFranceTriple.WebRole.Models.UsersContext();
                uc.Entry(userprofile).State = System.Data.EntityState.Modified;
                uc.SaveChanges();
                return RedirectToAction("Index", "User", new { area = "UserManager" });

            }
            return View(userprofile);
        }

        //
        // GET: /UserManager/User/Delete/5

        [Authorize(Roles = "RoleHR")]
        public ActionResult Delete(int id = 0)
        {
            UserProfile userprofile = db.UserProfile.Find(id);
            if (userprofile == null)
            {
                return HttpNotFound();
            }
            return View(userprofile);
        }

        //
        // POST: /UserManager/User/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleHR")]
        public ActionResult DeleteConfirmed(int id)
        {
            UserProfile userprofile = db.UserProfile.Find(id);
            db.UserProfile.Remove(userprofile);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}